//
//  ActivationViewController.m
//  JiuLe
//
//  Created by 黄鑫 on 16/8/26.
//  Copyright © 2016年 Fine. All rights reserved.
//

#import "ActivationViewController.h"
#import "IQKeyboardManager.h"
#import "Common.h"
#import "CLUIBaseViewController.h"
#import "BaseDataRequest.h"
#import "HomeViewController.h"
#import "BaseNavController.h"
#import "Balloon.h"
#import "NSString+Helper.h"
#import "NSString+Check.h"
#import <GTSDK/GeTuiSdk.h>

@interface ActivationViewController()
@property (weak, nonatomic) IBOutlet UITextField *textFieldUserName;
@property (weak, nonatomic) IBOutlet UITextField *textFieldUserPhone;
@property (weak, nonatomic) IBOutlet UITextField *textFieldNameOne;
@property (weak, nonatomic) IBOutlet UITextField *textFieldPhoneOne;
@property (weak, nonatomic) IBOutlet UITextField *textFieldNameTwo;
@property (weak, nonatomic) IBOutlet UITextField *textFieldPhoneTwo;

@end

@implementation ActivationViewController

- (void)viewDidLoad{
    [self showNavgationWithLeftItem];
    self.title = @"激活";
    [self showNavgationWithLeftItemWithTitle:@"返回"];
    //self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    self.navigationController.navigationBarHidden = NO;
    [IQKeyboardManager sharedManager].enable = NO;
}

- (void)viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:animated];
    
    [IQKeyboardManager sharedManager].enable = YES;
    
}

- (IBAction)activation:(id)sender {
    if (_textFieldUserName.text.length == 0) {
        TIPSFORVIEW(@"用户名不能为空！");
        return;
    }
    if(![_textFieldUserName.text checkUserPassword]){
        TIPSFORVIEW(@"用户名格式不正确");
        return;
    }
    if (_textFieldUserPhone.text.length == 0) {
        TIPSFORVIEW(@"手机号不能为空！");
        return;
    }

    if (![_textFieldUserPhone.text checkPhoneNumInput]) {
        TIPSFORVIEW(@"手机号格式不正确");
        return;
    }
    if(![_textFieldNameOne.text checkUserPassword]){
        TIPSFORVIEW(@"紧急联系人1姓名格式不正确");
        return;
    }
    if (![_textFieldPhoneOne.text checkPhoneNumInput]) {
        TIPSFORVIEW(@"紧急联系人1手机号格式不正确");
        return;
    }
    if(![_textFieldNameTwo.text checkUserPassword]){
        TIPSFORVIEW(@"紧急联系人2姓名格式不正确");
        return;
    }
    if (![_textFieldPhoneTwo.text checkPhoneNumInput]) {
        TIPSFORVIEW(@"紧急联系人2手机号格式不正确");
        return;
    }
    __weak ActivationViewController *weakSelf = self;
    [SVProgressHUD showWithStatus:@"激活中.." maskType:SVProgressHUDMaskTypeClear];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:_sequenceCode forKey:@"serial_number"];
    [dict setObject:_textFieldUserName.text forKey:@"name"];
    [dict setObject:_textFieldUserPhone.text forKey:@"mobile"];
    [dict setObject:_textFieldNameOne.text forKey:@"emergency_user_name1"];
    [dict setObject:_textFieldPhoneOne.text forKey:@"emergency_user_mobile1"];
    [dict setObject:_textFieldNameTwo.text forKey:@"emergency_user_name2"];
    [dict setObject:_textFieldPhoneTwo.text forKey:@"emergency_user_mobile2"];
    [[BaseDataRequest alloc] doactive:dict withBlock:^(id responseObject, NSString *status, NSError *error) {
        [SVProgressHUD dismiss];
        if ([status isEqualToString:@"0"]) {
            NSDictionary *result = [responseObject objectForKey:@"result"];
            [NSUserDefaults setObject:[result objectForKey:@"user_id"] forKey:UserDataID];
            [NSUserDefaults setObject:[result objectForKey:@"token"] forKey:UserToken];
            [NSUserDefaults setObject:[result objectForKey:@"serial_number"] forKey:UserCCID];
            [NSUserDefaults setObject:[NSString stringTime:[NSDate date]] forKey:ActivationDate];
            [NSUserDefaults setObject:[result objectForKey:@"serial_number"] forKey:UserDataName];
            [NSUserDefaults setObject:[result objectForKey:@"gender"]  forKey:UserSex];
            [NSUserDefaults setObject:@"170"  forKey:UserHeight];
            [NSUserDefaults setObject:@"60"  forKey:UserWeight];
            [NSUserDefaults setObject:[result objectForKey:@"serial_number"] forKey:TerminalEquipment];
            [[NSUserDefaults standardUserDefaults]setObject:[result objectForKey:@"ispush"] forKey:UserPush];
            if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"push"] boolValue] == YES || [[NSUserDefaults standardUserDefaults] objectForKey:@"push"] == NULL) {
                NSString *str = [NSString stringWithFormat:@"JL%@%@",[result objectForKey:@"serial_number"],[result objectForKey:@"user_id"]];
                [GeTuiSdk bindAlias:[[NSString string] md5:str]];
                [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"push"];
            }
            Balloon *balloon = [[Balloon alloc] init];
            [balloon showActivationSuccessful:@"激活成功，开始使用"];
            [weakSelf performSelector:@selector(setRootController)
                           withObject:@{@"bol":@(YES)}
                           afterDelay:1.5f];
        }else{
            if (error != nil) {
                TIPSFORVIEW(@"请检测网络设置");
            }else{
                TIPSFORVIEW(@"激活失败");
            }
        }
    }];
}

- (void)backTo{
    if ([self.from isEqualToString:@"2"]) {
        [self.navigationController popToRootViewControllerAnimated:YES];
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)setRootController {
    //跳转首页
    HomeViewController *home = [[HomeViewController alloc]initWithNibName:@"HomeViewController" bundle:nil];
    BaseNavController *baseNav = [[BaseNavController alloc] initWithRootViewController:home];
    [UIApplication sharedApplication].delegate.window.rootViewController = baseNav;
}

@end
